
import ic.ast.PrettyPrint;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
		String src = "";
		String lib = "";
		
		try {
			if (System.console() == null && System.in.available() > 0) {
				src += new Scanner(System.in).useDelimiter("\\Z").next();
			}
		} catch (IOException e1) {
		}
		
		if (args != null && args.length > 0) {
			try {
				src += new Scanner(new File(args[0])).useDelimiter("\\Z").next();
				
				if(args.length > 1)
				{
					if(args[1].startsWith("-L"))
					{
						lib += new Scanner(new File(args[1].substring(2))).useDelimiter("\\Z").next();
					}
				}
				
				// TODO: handle invalid arguments
				
			} catch (FileNotFoundException e) {
			}
		}

		try {
			if(!lib.isEmpty())
			{
				System.out.println(((new LibraryParser()).process((new Lexer()).process(lib))).accept(new PrettyPrint()));
			}
			
			System.out.println(((new ProgramParser()).process((new Lexer()).process(src))).accept(new PrettyPrint()));
			
		} catch (LexerException e) {
			System.out.println(e.getLine() + ":" + e.getColumn()  + " : lexical error; " + e.getMessage());
        } catch (ParserException e) {
            if (e.getLine() > 0) {
                System.out.println(e.getLine() + ":" + e.getColumn()  + " : syntax error; " + e.getMessage());
			}
            else {
                System.out.println("at end of input : syntax error; " + e.getMessage());
			}
        } catch (IOException e) {
        }
	}
}

